//Lockbox touch screen
//Corky Lederer
//lederer.ca@hotmail.com
//11/12/11
//Version 0.1

/*
Breadboard # = LCD PINOUT -> Arduino Pin
 1  = D7
 2  = D6
 3  = D5
 4  = D4
 5  = D3
 6  = D2
 7  = D1
 8  = D0
 9  = Y- ---------> D2
 10 = X- ---------> A9
 11 = Y+ ---------> A8
 12 = X+ ---------> D3
 13 = Backlight --> 5V
 14 = Reset ------> Reset
 15 = RD
 16 = Wr
 17 = C/D
 18 = Cs
 19 = 5V
 20 = GND --------> GND
 */

#include <TFTLCD.h>
#include <TouchScreen.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>

//Define pins for touchscreen
#define YP A8  // must be an analog pin, use "An" notation!
#define XM A9 // must be an analog pin, use "An" notation!
#define YM 2   // can be a digital pin
#define XP 3   // can be a digital pin

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0 
// optional
#define LCD_RESET A4

// Color definitions
int black = 0x0000;
int blue = 0x001F;
int red = 0xF800;
int green = 0x6600; //07E0
int cyan = 0x07FF;
int magenta = 0xF81F;
int yellow = 0xFFE0;
int white = 0xFFFF;

TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

//end of prebuilt code
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//Corky's vars
int xc = -1;
int yc = -1;

//Key statuses
boolean millKeyStatus = false;   //true = in 
boolean latheKeyStatus = false;  //true = in
int millStatusColor = white;
int latheStatusColor = white;


//Other Statuses
boolean authUser = false;
boolean admin = false;
long latheUser = 0;                            //NEED TO IMP
long millUser = 0;                             //NEED TO IMP


//############## RFID ##################
//Data0 is -- Pin 19 -- 4 -- Green
//Data1 is -- Pin 18 -- 5 -- White

long previousMillis = 0;
int interval = 3000;
volatile unsigned long bit_holder = 0;
volatile int bit_count = 0;

long RFID = -1;

//Debug LED
int dled = 13;

//############# SD Card ##################
//SD Card PIN OUT
//CLK 52
//DO 50
//DI 51

//Pins
int SDcs = 7;                //Chip select pin

//Files
File users;                 //Users file
File keyStatus;             //KeyStatusFile
File chold;                 //Current Key Holders
File admins;                //Admins 
File elog;                  //eventlog; 

//Vars
char SDIncoming;            //read byte from file
int SDi = 0;                //read buffer index
char SDRFIDbuffer [6];      //RFID buffer
long SDRFID;                //SD Read RIFD #
char SDStudentID[10];       //SD Read Student #
char SDname[60];            //SD Read Studnet Name

//############# Clock ##################
RTC_DS1307 RTC;
DateTime time;

/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

void setup()
{
  Serial.begin(9600);
  Serial.println("8 Bit LCD test!");

  //Reset LCD
  tft.reset();

  //Determine Chip
  uint16_t identifier = tft.readRegister(0x0);
  if (identifier == 0x9325) {
    Serial.println("Found ILI9325");
  } 
  else if (identifier == 0x9328) {
    Serial.println("Found ILI9328");
  } 
  else {
    Serial.print("Unknown driver chip ");
    Serial.println(identifier, HEX);
    while (0);
  } 
  //Set rotation
  tft.setRotation(3); 

  //Initalize LCD and set screen
  tft.initDisplay();

  //RFID Setup
  clearinterrupts();
  attachInterrupt(4, DATA0, RISING);
  attachInterrupt(5, DATA1, RISING);
  delay(10);

  //Clock Setup
  Wire.begin();
  RTC.begin();
  RTC.adjust(DateTime(__DATE__, __TIME__)); //set date and time on compile


  //Read indication LED
  pinMode(dled,OUTPUT);
  digitalWrite(dled,LOW);

  //SD Setup
  pinMode(SDcs,OUTPUT);

  if(!SD.begin(SDcs))
  {
    Serial.println("SD Card Initializaion Failed!");
    tft.setTextSize(1);
    tft.fillScreen(red);
    tft.println("SD Card Initializaion Failed!");
    while(true)
    {
    }
  }
  else
  {
    Serial.println("SD Card Initializaion Done!");
  }

  //initalize SD vars
  SDRFID = 0;
  for(int jk = 0; jk < 10; jk++)
  {
    SDStudentID[jk] = '0';
  }
  for(int cnk = 0; cnk < 60; cnk++)
  {
    SDname[cnk] = '0';
  }

  updateLog(6);
  getKeyStatus();
  getCurrentHolders();
  clearWindow();
  statusScreen(); //Goes to status screen
}
void loop()
{
  //loop nothing
}

void clearWindow()
{
  tft.fillScreen(black);
}
void statusScreen()
{
  clearWindow();
  getColors();
  tft.setTextSize(2);
  tft.setCursor(80,10);
  tft.println("Machine Status");
  tft.fillRect(50, 50, 100, 50, millStatusColor);
  tft.setCursor(80,70);
  tft.println("Mill");
  tft.fillRect(170,50,100,50,latheStatusColor);
  tft.setCursor(195,70);
  tft.println("Lathe");
  tft.setCursor(75,150);
  tft.setTextSize(3);
  tft.println("Swipe card");
  tft.setCursor(75,190);
  tft.println("for access");

  while(true)
  {
    // getPoint();


    getID();
    if(RFID != -1)
    {
      auth();
      //if the ID read in is in the user database the screen is advanced to selectMachine()
      if(authUser == true && SDRFID != latheUser && SDRFID != millUser)
      {
        authUser = false;
        selectMachine();
      }
      if(authUser == true && SDRFID == latheUser)
      {
        authUser = false;
        returnLatheKey();
      }
      if(authUser == true && SDRFID == millUser)
      {
        authUser = false;
        returnMillKey();
      }
      //if the ID read in is not in the user database the denined screen is shown
      else
      {
        denied();
      }
    }
  }
}

void selectMachine ()
{

  clearWindow();
  getColors();
  tft.setTextSize(2);
  tft.setCursor(85,55);
  tft.println("Choose Machine");
  tft.setCursor(100,75);
  tft.println("To Check Out");
  tft.setTextSize(1);
  tft.setCursor(tft.width()-35,tft.height()-10);
  tft.println("Admin");
  tft.setTextSize(2);
  tft.fillRect(115, 185, 100,50 ,blue);
  tft.setCursor(145, 205);
  tft.println("Back");

  if(millKeyStatus == false && latheKeyStatus == false)
  {
    tft.setCursor(70,145);
    tft.println("No Keys Available");
  }

  if(millKeyStatus == true)
  {
    tft.fillRect(40, 125, 100, 50, millStatusColor);
    tft.setCursor(70,145);
    tft.println("Mill");
  }
  if(latheKeyStatus == true)
  {
    tft.fillRect(180,125,100,50, latheStatusColor);
    tft.setCursor(205,145);
    tft.println("Lathe");
  }
  while(true)
  {
    getPoint();
    //choose mill
    if(xc >40 && xc <140 && yc >130 && yc <180 && millKeyStatus == true )
    {
      removeMillKey();
    }
    //choose lathe
    if(xc > 180 && xc < 280 && yc > 130 && yc < 180 && latheKeyStatus == true)
    {
      removeLatheKey();
    }
    //choose back
    if(xc > 100 && xc < 200 && yc > 185 && yc < 235) 
    {
      statusScreen();
    }

    if(xc >tft.width()-50 && xc < tft.width() && yc > tft.height()-20 && yc < tft.height())
    {
      enterAdmin();
    }
  }
}
void removeMillKey()
{
  tft.fillScreen(green);
  tft.setCursor(35,100);
  tft.setTextSize(3);
  tft.println("Remove Mill Key");
  millKeyStatus = false;
  millUser = SDRFID;
  updateKeyStatus();
  updateCurrentHolders();
  updateLog(2);
  delay(1500);

  //inser removal sequecne
  statusScreen();
}
void removeLatheKey()
{
  tft.fillScreen(green);
  tft.setCursor(20,100);
  tft.setTextSize(3);
  tft.println("Remove Lathe Key");
  latheKeyStatus = false;
  latheUser = SDRFID;
  updateKeyStatus();
  updateCurrentHolders();
  updateLog(4);
  delay(1500);
  //insert removal sequecne
  statusScreen();
}

void returnLatheKey()
{
  tft.fillScreen(blue);
  tft.setCursor(20,100);
  tft.setTextSize(3);
  tft.println("Return Lathe Key");
  latheKeyStatus = true;
  latheUser = 0;
  updateKeyStatus();
  updateCurrentHolders();
  updateLog(3);
  delay(1500);
  //insert return sequence
  statusScreen();
}

void returnMillKey()
{
  tft.fillScreen(blue);
  tft.setCursor(20,100);
  tft.setTextSize(3);
  tft.println("Return Mill Key");
  millKeyStatus = true;
  millUser = 0;
  updateKeyStatus();
  updateCurrentHolders();
  updateLog(1);
  delay(1500);
  //insert return sequence
  statusScreen();
}

void enterAdmin()
{
  tft.fillScreen(blue);
  tft.setCursor(45,100);
  tft.setTextSize(3);
  tft.println("Authorizing...");
  checkAdmin();
  delay(2000);

  if(admin == true)
  {
    admin = false;
    keyAdmin();
  }
  else
  {
    admin = false;
    denied();
  }
}
void keyAdmin()
{  
  clearWindow();
  tft.setTextSize(2);

  //Mill Options
  tft.setCursor(60,10);
  tft.println("Mill");
  tft.fillRect(30, 40, 100, 50, green);
  tft.setCursor(60,60);
  tft.println("Open");
  tft.fillRect(30, 100, 100, 50, green);
  tft.setCursor(45,120);
  tft.println("Key in");
  tft.fillRect(30, 162, 100, 50, green);
  tft.setCursor(40,182);
  tft.println("Key out");

  //Divider
  tft.drawLine(tft.width()/2,0,tft.width()/2,tft.height()-22,white);

  //Lathe Options
  tft.setCursor(220,10);
  tft.println("Lathe");
  tft.fillRect(195,40,100,50,green);
  tft.setCursor(220,60);
  tft.println("Open");
  tft.fillRect(195, 100, 100, 50, green);
  tft.setCursor(210, 120);
  tft.println("Key in");
  tft.fillRect(195, 162, 100, 50, green);
  tft.setCursor(205,182);
  tft.println("Key out");

  //Exit admin mode
  tft.fillRect(tft.width()/2-20,tft.height()-20,43,20,blue);
  tft.setTextSize(1);
  tft.setCursor(tft.width()/2-10,tft.height()-12);
  tft.println("BACK");

  //Admin mode tag
  tft.setCursor(5,tft.height()-10);
  tft.println("Admin Mode");

  while(true)
  {
    getPoint();

    //mill key in
    if(xc >30 && xc < 130 && yc > 100 && yc < 150)
    {
      millKeyIn();
    }

    //mill key out
    if(xc >30 && xc < 130 && yc > 162 && yc < 212)
    {
      millKeyOut();
    }
    //lathe key in
    if(xc >195 && xc < 295 && yc > 100 && yc < 150)
    {
      latheKeyIn();
    }

    //lathe key out
    if(xc >195 && xc < 295 && yc > 162 && yc < 212)
    {
      latheKeyOut();
    }
    if(xc >tft.width()/2-20 && xc < tft.width()/2+23 && yc > tft.height()-20 && yc < tft.height())
    {
      statusScreen();
    }

  }
}
void millKeyIn ()
{
  clearWindow();
  tft.setCursor(30,100);
  tft.setTextSize(2);
  tft.println("Mill key marked as in");
  millKeyStatus = true;
  millUser = 0;
  updateKeyStatus();
  updateCurrentHolders();
  delay(1500);
  keyAdmin();
}

void millKeyOut ()
{
  clearWindow();
  tft.setCursor(30,100);
  tft.setTextSize(2);
  tft.println("Mill key marked as out");
  millKeyStatus = false;
  millUser = 0;
  updateKeyStatus();
  updateCurrentHolders();
  delay(1500);
  keyAdmin();
}

void latheKeyIn ()
{
  clearWindow();
  tft.setCursor(20,100);
  tft.setTextSize(2);
  tft.println("Lathe key marked as in");
  latheKeyStatus = true;
  latheUser = 0;
  updateKeyStatus();
  updateCurrentHolders();
  delay(1500);
  keyAdmin();
}

void latheKeyOut ()
{
  clearWindow();
  tft.setCursor(20,100);
  tft.setTextSize(2);
  tft.println("Lathe key marked as Out");
  latheKeyStatus = false;
  latheUser = 0;
  updateKeyStatus();
  updateCurrentHolders();
  delay(1500);
  keyAdmin();
}

void getPoint()
{
  yc = -1;
  xc= -1;

#define MINPRESSURE 500
#define MAXPRESSURE 1000

  Point p = ts.getPoint();
  pinMode(XM, OUTPUT);
  pinMode(YP, OUTPUT);
  if (p.z > MINPRESSURE && p.z < MAXPRESSURE) 

    //debug
    /////////////////////////////////////////
    /*
  Serial.println(p.x);
     Serial.println(p.y);
     Serial.println(p.z);
     Serial.println();
     */
    //////////////////////////////////////
  {
    //yc = tft.height()- map(p.x,TS_MINY, TS_MAXY , tft.height(), 0);
   // xc = map(p.y,TS_MINX, TS_MAXX , tft.width(), 0);
   yc = tft.height()- map(p.x,TS_MAXY, TS_MINY , tft.height(), 0);
   xc = map(p.y,TS_MAXX,TS_MINX, tft.width(), 0);
  }
}

void denied()
{
  tft.fillScreen(red);
  tft.setCursor(45,100);
  tft.setTextSize(3);
  tft.println("Access Denied");
  tft.setCursor(35,150);
  tft.setTextSize(1);
  tft.println("Contact administrator if this is incorrect");
  updateLog(5);
  delay(2500);
  statusScreen();
}

//Machine Status Colors
void getColors()
{
  if(millKeyStatus == true)
  {
    millStatusColor = green;
  }
  if(millKeyStatus == false)
  {
    millStatusColor = red;
  }
  if(latheKeyStatus == true)
  {
    latheStatusColor = green;
  }
  if(latheKeyStatus == false)
  {
    latheStatusColor = red;
  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//RFID Methods

void getID()
{
  //clear buffer after timeout
  if (millis() - previousMillis > interval) {
    bit_count = 0; 
    bit_holder = 0;
    previousMillis = millis();
  }

  //full read resutlts in 35 bits
  if (bit_count == 35) {
    previousMillis = millis();

    //shift right so that result matches what is printed on the card
    bit_holder = bit_holder >> 1;

    //take only the lower 20 bits
    bit_holder &= 0xFFFFF;
    Serial.println(bit_holder);
    RFID = bit_holder;
    Serial.println(bit_holder);
    bit_count = 0; 
    bit_holder = 0;

    delay(10);
  }
  delay(50);
}



void clearinterrupts () {
  // the interrupt in the Atmel processor mises out the first negitave pulse as the inputs are already high,
  // so this gives a pulse to each reader input line to get the interrupts working properly.
  // Then clear out the reader variables.
  // The readers are open collector sitting normally at a one so this is OK
  for(int i = 2; i<4; i++){
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH); // enable internal pull up causing a one
    digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
    pinMode(i, INPUT);
    digitalWrite(i, HIGH); // enable internal pull up
  }
  delay(10);
}

void DATA0(void) {
  bit_count++;
  bit_holder = bit_holder << 1;
}

void DATA1(void) 
{
  bit_count++;
  bit_holder = bit_holder << 1;
  bit_holder |= 1;
}

//Check RFID agianst known users
void auth()
{
  Serial.println("Authorizing User...");
  users = SD.open("usr.txt", FILE_READ);
  if(users)
  {
    while(users.available() && authUser == false)
    {
      SDRFID = 0;
      for(int pt = 0; pt < 10; pt++)
      {
        SDStudentID[pt] = '0';
      }
      for(int ck = 0; ck < 60; ck++)
      {
        SDname[ck] = '0';
      }
      SDi = 0;
      SDIncoming = 'nul';
      while(SDi < 76 && SDIncoming != ';')
      {
        SDIncoming = users.read();

        //Insert possible error check

        if(SDIncoming != ',' && SDIncoming !=';' && SDi<6)
        {
          SDRFIDbuffer[SDi] = SDIncoming;
        }

        if(SDIncoming != ',' && SDIncoming !=';' && SDi > 6 && SDi <17)
        {
          SDStudentID[SDi-7] = SDIncoming;
        }

        if(SDi>17)
        {
          SDname[SDi-18] = SDIncoming;
        }
        SDi++;
      }
      //Read extra chars on line
      users.read();
      users.read();

      //Cast RFID Buffer to long
      SDRFID = atol(SDRFIDbuffer);
      Serial.println(SDRFID);
      if (SDRFID == RFID)
      {
        authUser = true;
      }
    }
  }
  else
  {
    tft.fillScreen(red);
    tft.setTextSize(1);
    tft.setCursor(0,0);
    tft.println("File Error: Could not open usr.txt");
    while(true)
    {
      //loop indef
    }
  }
  users.close();
  RFID = -1;
}

void getKeyStatus ()
{
  int SDmill = 0;
  int SDlathe = 0;
  //file format should be 1 line consinsting of <millKeyStatus (1 || 0)>,<latheKeyStatus (1 || 0)>
  //where 1 indicates key is in and 0 indicates out
  keyStatus = SD.open("keys.txt", FILE_READ);
  if(keyStatus)
  {
    SDmill = keyStatus.read()-48; //covert to ascii
    keyStatus.read();
    SDlathe = keyStatus.read()-48; //conver to ascii
    Serial.println(SDmill);
    Serial.println(SDlathe);

    //format error handling
    if(SDmill > 1 || SDmill < 0 || SDlathe > 1 || SDlathe < 0)
    {
      tft.fillScreen(red);
      tft.setCursor(0,0);
      tft.setTextSize(1);
      tft.println("File Error: keys.txt format incorrect");
      delay(5000);
    }
    else
    {
      if(SDmill == 1)
      {
        millKeyStatus = true;
      }
      else if (SDmill == 0)
      {
        millKeyStatus = false;
      }
      if (SDlathe == 1)
      {
        latheKeyStatus = true;
      }
      else if (SDlathe == 0)
      {
        latheKeyStatus = false;
      }
    }
    keyStatus.close();
  }
  else
  {
    keyStatus.close(); // not sure if this is suppose to be here
    tft.fillScreen(red);
    tft.setCursor(0,0);
    tft.setTextSize(1);
    tft.println("File Error: Cannot open keys.txt");
    delay(5000);
  }
}

void updateKeyStatus()
{
  keyStatus = SD.open("keys.txt", FILE_WRITE);
  if(keyStatus)
  {
    keyStatus.seek(0);
    if(millKeyStatus == true)
    {
      keyStatus.print('1');
    }
    else if (millKeyStatus == false)
    {
      keyStatus.print('0');
    }

    keyStatus.print(',');

    if(latheKeyStatus == true)
    {
      keyStatus.print('1');
    }
    else if(latheKeyStatus == false)
    {
      keyStatus.print('0');
    }
    keyStatus.close();
  }
  else
  {
    keyStatus.close(); // not sure if this is suppose to be here
    tft.fillScreen(red);
    tft.setCursor(0,0);
    tft.setTextSize(1);
    tft.println("File Error: Cannot open keys.txt");
    delay(5000);
  }

}

void getCurrentHolders()
{
  char readUChrs [6];
  char curRead;
  chold = SD.open("cuser.txt", FILE_READ);
  if(chold)
  {
    for(int z = 0; z < 6; z++)
    {
      curRead = chold.read();
      Serial.print(curRead);
      if(curRead > '9'|| curRead < '0')
      {
        chold.close();
        tft.fillScreen(red);
        tft.setCursor(0,0);
        tft.setTextSize(1);
        tft.println("File Error: cuser.txt format incorrect");
        while(true)
        {
          //loop indef
        }
      }
      else
      {
        readUChrs[z] = curRead;
      }
    }
    millUser = atol(readUChrs);    
    chold.read();
    Serial.println();

    for(int y = 0; y < 6; y++)
    {
      curRead = chold.read();
      Serial.print(curRead);
      if(curRead > '9' || curRead < '0')
      {
        chold.close();
        tft.fillScreen(red);
        tft.setCursor(0,0);
        tft.setTextSize(1);
        tft.println("File Error: cuser.txt format incorrect");
        while(true)
        {
          //loop indef
        }
      }
      else
      {
        readUChrs[y] = curRead;
      }

    }
    latheUser = atol(readUChrs);
    chold.close();
    Serial.println(latheUser);
    Serial.println(millUser);
  }
  else
  {
    chold.close(); // not sure if this is suppose to be here
    tft.fillScreen(red);
    tft.setCursor(0,0);
    tft.setTextSize(1);
    tft.println("File Error: Cannot open cuser.txt");
    while(true)
    {
      //loop indef
    }
  }
}

void updateCurrentHolders()
{
  chold = SD.open("cuser.txt", FILE_WRITE);
  if(chold)
  {
    chold.seek(0);
    if(millUser == 0)
    {
      for(int x = 0; x < 6; x++)
      {
        chold.print('0');
      }
    }
    else
    {
      chold.print(millUser);
    }

    //comma
    chold.print(',');

    if(latheUser == 0)
    {
      for(int x = 0; x < 6; x++)
      {
        chold.print('0');
      }
    }
    else
    {
      chold.print(latheUser);
    }
    chold.close();
  }
  else
  {
    chold.close(); // not sure if this is suppose to be here
    tft.fillScreen(red);
    tft.setCursor(0,0);
    tft.setTextSize(1);
    tft.println("File Error: Cannot open cuser.txt");
    while(true)
    {
      //loop indef
    }
  }
}

//need to add in loop for mulitple admins
void checkAdmin()
{
  char adminID[6];
  char adminHold;
  long readAdmin = 0;  
  admins = SD.open("admins.txt", FILE_READ);

  if(admins)
  {
    for(int w = 0; w < 6; w++)
    {
      adminHold = admins.read();

      if(adminHold > '9' || adminHold < '0')
      {
        tft.fillScreen(red);
        tft.setCursor(0,0);
        tft.setTextSize(1);
        tft.println("File Error: Cannot open admins.txt");
        delay(5000);
        //break;
      }
      else
      {
        adminID[w] = adminHold;
      }
    }
    admins.close();
    readAdmin = atol(adminID);

    if(readAdmin == SDRFID)
    {
      admin = true;
    }
    else
    {
      admin = false;
    }
  }
  else
  {
    admins.close(); // not sure if this is suppose to be here
    tft.fillScreen(red);
    tft.setCursor(0,0);
    tft.setTextSize(1);
    tft.println("File Error: Cannot open admins.txt");
    delay(5000);
  }
}

void updateLog(int message)
{
  int r = 0;
  elog = SD.open("elog.txt", FILE_WRITE);
  time = RTC.now();
  if(elog)
  {
    elog.print(time.day(), DEC);
    elog.print('/');
    elog.print(time.month(), DEC);
    elog.print('/');
    elog.print(time.year(), DEC);
    elog.print(' ');
    elog.print(time.hour(), DEC);
    elog.print(':');
    elog.print(time.minute(), DEC);
    elog.print(':');
    elog.print(time.second(), DEC);
    elog.print(' ');
    if(message == 1 || message == 2 || message == 3 || message == 4)
    {
      while(SDname[r] != ';')
      {
        elog.print(SDname[r]);
        r++;
      }
      elog.print(" : ");
    }

    //Need to add some sort of indicaion on acion possibly a switch statement
    switch(message)
    {
    case 1:
      elog.print("mill in");
      break;

    case 2:
      elog.print("mill out");
      break;

    case 3:
      elog.print("lathe in");
      break;

    case 4:
      elog.print("lathe out");
      break;

    case 5:
      elog.print("Access Denied");
      break;

    case 6:
      elog.print("Reset");
      break;

    default:
      elog.print("No Message");  
    }

    elog.println();
    elog.close();

  }
  else
  {
    elog.close(); // not sure if this is suppose to be here
    tft.fillScreen(red);
    tft.setCursor(0,0);
    tft.setTextSize(1);
    tft.println("File Error: Cannot open elog.txt");
    delay(5000);
  }
  //Clear read SD info
  SDRFID = 0;
  for(int q = 0; q < 10; q++)
  {
    SDStudentID[q] = '0';
  }
  for(int cn = 0; cn < 60; cn++)
  {
    SDname[cn] = '0';
  }
}
